package com.ad.pet.ui;

import android.app.Dialog;
import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.ad.pet.R;
import com.ad.pet.app.recharge.RechargeAdapter;
import com.ad.pet.models.remote.account.order.PayMethod;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liuyixiang on 2019/12/12.
 */

public class PayChoseDialog extends Dialog {
    private int screenWidth;
    OnGetPayWay onGetPayWay;
    boolean isService;
    public PayChoseDialog(Context context, int themeResId,boolean isService) {
        super(context, themeResId);
        this.isService = isService;
        screenWidth = context.getResources().getDisplayMetrics().widthPixels;
        View mView = View.inflate(context, R.layout.layout_pay_item,null);

        setContentView(mView);

        RecyclerView recyclerView=mView.findViewById(R.id.recycler_sku);
        final PayMethod payMethod = new PayMethod();
        payMethod.setChose(true);
        payMethod.setIcon(R.drawable.ic_detail_zhifubao);
        payMethod.setPayment_method("支付宝支付");
        PayMethod payMethod1 = new PayMethod();
        payMethod1.setChose(false);
        payMethod1.setIcon(R.drawable.ic_detail_weixin);
        payMethod1.setPayment_method("微信支付");


        List<PayMethod > payMethods = new ArrayList<>();
        payMethods.add(payMethod);
        payMethods.add(payMethod1);
        if (isService){
                PayMethod payMethod2 = new PayMethod();
                payMethod2.setChose(false);
                payMethod2.setIcon(R.drawable.ic_yue);
                payMethod2.setPayment_method("余额支付");
            payMethods.add(payMethod2);
        }

        final RechargeAdapter rechargeAdapter = new RechargeAdapter( payMethods);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setAdapter(rechargeAdapter);

        rechargeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                rechargeAdapter.setChoseRecharge(position);
            }
        });

        mView.findViewById(R.id.tv_door).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PayMethod payMethod2 =rechargeAdapter.getChoseRecharge();
                if ("微信支付".equals(payMethod2.getPayment_method())){
                    onGetPayWay.getPayWay(1);
                }else if ("支付宝支付".equals(payMethod2.getPayment_method())){
                    onGetPayWay.getPayWay(0);
                }else {
                    onGetPayWay.getPayWay(2);
                }
            }
        });
        mView.findViewById(R.id.iv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    public void setOnGetPayWay(OnGetPayWay onGetPayWay) {
        this.onGetPayWay = onGetPayWay;
    }

    @Override
    public void show() {
        super.show();
        Window window = this.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.height= ViewGroup.LayoutParams.WRAP_CONTENT;
        lp.width= ViewGroup.LayoutParams.MATCH_PARENT;
//        lp.height  = LyxUtils.dp2px(getContext(),430);
//        lp.width = (screenWidth *  8/ 10); // 设置宽度
        this.getWindow().setAttributes(lp);
        window.setGravity(Gravity.BOTTOM);
    }

    public interface OnGetPayWay{
        void getPayWay(int way);
    }
}
